//
//  RegisterViewController.m
//  duoduo
//
//  Created by wanghao on 16/10/12.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "RegisterViewController.h"
#import "CheckViewController.h"
#import "UIButton+timer.h"

@interface RegisterViewController ()
{
    NSInteger count;
    NSString *_verCode;
    NSString *_phoneNum;
}
@property (weak, nonatomic) IBOutlet UIButton *selectBtn;
@property (weak, nonatomic) IBOutlet UIButton *xieyiBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *yanzhengmaBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"注册账号";
    count = 1;
    [self initUI];
}
- (void)initUI
{
    self.view.backgroundColor = LXHexColor(0xF5F6F7);

    //验证码按钮
    [_yanzhengmaBtn setTitleColor:LXHexColor(0x5DCA68) forState:UIControlStateNormal];
    [_yanzhengmaBtn setTitleColor:LXHexColor(0x5DCA68) forState:UIControlStateSelected];
    [_yanzhengmaBtn addTarget:self action:@selector(yangzhengmaClick) forControlEvents:UIControlEventTouchUpInside];

    _selectBtn.layer.borderColor = [UIColor grayColor].CGColor;
    _selectBtn.layer.borderWidth = 1;
    [_selectBtn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    //用户协议按钮
    [_xieyiBtn setTitleColor:LXHexColor(0x5DCA68) forState:UIControlStateNormal];

    [_xieyiBtn addTarget:self action:@selector(xieyiClick) forControlEvents:UIControlEventTouchUpInside];

    //登录按钮
    _registerBtn.backgroundColor = LXHexColor(0x5DCA68);
    _registerBtn.layer.cornerRadius = 6;
    _registerBtn.layer.masksToBounds = YES;
    [_registerBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
}
//去审核
- (IBAction)gotoSHClick:(id)sender {
    CheckViewController * check = [[CheckViewController alloc]init];
    [self.navigationController pushViewController:check animated:YES];
}
//用户协议
- (void)xieyiClick
{

}
//获取验证码
- (void)yangzhengmaClick
{
    if (!_phoneField.text.length) {
        [HUDTool showInView:self.view withText:@"请输入手机号"];
        return;
    }
    if (![QZChectTool isPhoneNumber:_phoneField.text]) {
        [HUDTool showInView:self.view withText:@"请输入正确格式的手机号"];
        return;
    }
    __weak typeof(self) weakSelf = self;
    //先判断手机是否已注册
    NSDictionary *dic = @{@"mobile":_phoneField.text,
                          @"token":@"Jg6uH5ZAkksqaFco"};
    [MHNetworkManager postReqeustWithURL:URL(@"?m=appface&c=sms&a=send_reg_code") params:dic successBlock:^(NSDictionary *returnData) {
        NSLog(@"returnData = %@",returnData);
        if (ResultJudge) {
            _verCode = returnData[@"data"][@"code"];
            _phoneNum = _phoneField.text;

            [weakSelf.yanzhengmaBtn addTimer];
        }else{
            ShowResultMsg;
        }
    } failureBlock:^(NSError *error) {
        DTLog(@"error = %@",error);
        ShowFailMsg;
    } showHUD:YES];
}
//注册
- (void)registerClick
{
    if (!_phoneField.text.length) {
        ShowSomeMsg(@"请输入手机号码");
        return;
    }
    if (![_phoneField.text isEqualToString:_phoneNum]) {
        ShowSomeMsg(@"请输入获取验证码的手机号");
        return;
    }
    if (!_vercodeField.text.length) {
        ShowSomeMsg(@"请输入验证码");
        return;
    }
    if (![_vercodeField.text isEqualToString:_verCode]) {
        ShowSomeMsg(@"验证码不正确");
        return;
    }
    if (!_passwordField.text.length) {
        ShowSomeMsg(@"请输入密码");
        return;
    }
    __weak typeof(self) weakSelf = self;
    //先判断手机是否已注册
    NSDictionary *dic = @{@"mobile":_phoneField.text,
                          @"pwd":_passwordField.text};
    [MHNetworkManager postReqeustWithURL:URL(@"?m=appface&c=user&a=reg") params:dic successBlock:^(NSDictionary *returnData) {
        NSLog(@"returnData = %@",returnData);
        if (ResultJudge) {
            ShowSomeMsg(@"注册成功");

            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            ShowResultMsg;
        }
    } failureBlock:^(NSError *error) {
        DTLog(@"error = %@",error);
        ShowFailMsg;
    } showHUD:YES];
}
- (void)selectClick:(UIButton *)sender
{

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
